# -*- coding: utf-8 -*-
# @project: huawang-api20230914
# @file： tmp_lzx_002.py
# @Time：2023/8/1 14:59
# @Author：lizhixiong
import pygame
import random

"""
请注意，上述代码中使用了两张图片，一张是飞机的图片（plane.png），
另一张是敌机的图片（enemy.png）。确保在运行代码之前，将这两张图片放在与代码相同的目录下，以便正确加载图片。
保存以上代码为一个.py文件，然后运行该文件，即可启动打飞机游戏。在游戏中，
你可以使用左右方向键控制飞机的移动，按下空格键发射子弹，击中敌机得分，游戏目标是尽可能多地击落敌机并获得高分。
"""

pygame.init()

# 游戏窗口大小
SCREEN_WIDTH, SCREEN_HEIGHT = 640, 480

# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)

# 初始化游戏窗口
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("打飞机游戏")


# 飞机类
class Plane:
    def __init__(self):
        self.image = pygame.image.load("plane.png")
        self.rect = self.image.get_rect()
        self.rect.centerx = SCREEN_WIDTH // 2
        self.rect.bottom = SCREEN_HEIGHT - 10
        self.speed = 5

    def move_left(self):
        self.rect.x -= self.speed

    def move_right(self):
        self.rect.x += self.speed

    def draw(self):
        screen.blit(self.image, self.rect)


# 子弹类
class Bullet:
    def __init__(self, x, y):
        self.rect = pygame.Rect(x, y, 5, 10)
        self.speed = 10

    def move(self):
        self.rect.y -= self.speed

    def draw(self):
        pygame.draw.rect(screen, RED, self.rect)


# 敌机类
class Enemy:
    def __init__(self):
        self.image = pygame.image.load("enemy.png")
        self.rect = self.image.get_rect()
        self.rect.x = random.randint(0, SCREEN_WIDTH - self.rect.width)
        self.rect.y = random.randint(-100, -40)
        self.speed = random.randint(2, 5)

    def move(self):
        self.rect.y += self.speed

    def draw(self):
        screen.blit(self.image, self.rect)


# 主函数
def main():
    clock = pygame.time.Clock()
    plane = Plane()
    bullets = []
    enemies = []
    score = 0

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                return

        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            plane.move_left()
        if keys[pygame.K_RIGHT]:
            plane.move_right()

        # 发射子弹
        if keys[pygame.K_SPACE]:
            bullet = Bullet(plane.rect.centerx, plane.rect.top)
            bullets.append(bullet)

        # 生成敌机
        if random.randint(1, 50) == 1:
            enemy = Enemy()
            enemies.append(enemy)

        for bullet in bullets:
            bullet.move()
            if bullet.rect.bottom < 0:
                bullets.remove(bullet)

        for enemy in enemies:
            enemy.move()
            if enemy.rect.top > SCREEN_HEIGHT:
                enemies.remove(enemy)

        for enemy in enemies:
            for bullet in bullets:
                if enemy.rect.colliderect(bullet.rect):
                    enemies.remove(enemy)
                    bullets.remove(bullet)
                    score += 10

        screen.fill(BLACK)
        plane.draw()
        for bullet in bullets:
            bullet.draw()
        for enemy in enemies:
            enemy.draw()

        # 显示得分
        font = pygame.font.SysFont(None, 36)
        text = font.render("Score: " + str(score), True, WHITE)
        screen.blit(text, (10, 10))

        pygame.display.flip()
        clock.tick(60)


if __name__ == "__main__":
    main()
